<template>
  <div class="login-container">
    <div class="background-overlay"></div>
    <div class="content-wrapper">
      <el-card class="login-card">
        <template #header>
          <div class="card-header">
            <h2>探途旅行</h2>
            <p>开启您的精彩旅程</p>
          </div>
        </template>
        
        <el-form :model="loginForm" :rules="rules" ref="loginFormRef">
          <el-form-item prop="email">
            <el-input 
              v-model="loginForm.email"
              :prefix-icon="Message"
              placeholder="请输入邮箱"
              class="custom-input"
            />
          </el-form-item>
          
          <el-form-item prop="password">
            <el-input 
              v-model="loginForm.password"
              :prefix-icon="Lock"
              type="password"
              placeholder="请输入密码"
              show-password
              class="custom-input"
            />
          </el-form-item>
          
          <div class="remember-forgot">
            <el-checkbox v-model="rememberMe">记住我</el-checkbox>
            <el-link type="primary">忘记密码？</el-link>
          </div>
          
          <el-form-item>
            <el-button type="primary" @click="handleLogin" :loading="loading" class="submit-btn">
              立即登录
            </el-button>
          </el-form-item>
          
          <div class="form-footer">
            <el-link type="primary" @click="$router.push('/register')" class="register-link">
              还没有账号？立即注册
            </el-link>
          </div>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import type { FormInstance, FormRules } from 'element-plus'
import { Lock, Message } from '@element-plus/icons-vue'
import type { LoginForm } from '@/types/tuil'
import { authApi } from '@/api'

const router = useRouter()
const loginFormRef = ref<FormInstance>()
const loading = ref<boolean>(false)
const rememberMe = ref<boolean>(false)

const loginForm = reactive<LoginForm>({
  email: '',
  password: ''
})

const rules = reactive<FormRules>({
  email: [
    { required: true, message: '请输入邮箱地址', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ]
})

const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  try {
    const valid = await loginFormRef.value.validate()
    if (valid) {
      loading.value = true
      // TODO: 实现登录逻辑
      await authApi.login(loginForm)
      router.push('/')
    }
  } catch (error) {
    console.error('登录验证失败:', error)
  } finally {
    loading.value = false
  }
}

</script>

<style scoped>
.login-container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-image: url('@/assets/travel-bg.jpg');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

.background-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.4);
  backdrop-filter: blur(8px);
}

.content-wrapper {
  position: relative;
  z-index: 1;
  animation: slideUp 0.8s ease-out;
  width: 100%;
  max-width: 420px;
  margin: 0 20px;
}

.login-card {
  width: 100%;
  border-radius: 20px;
  backdrop-filter: blur(10px);
  background: rgba(255, 255, 255, 0.95);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  transition: all 0.3s ease;
}

/* 动画效果 */
@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.card-header {
  text-align: center;
  padding: 30px 0;
  background: linear-gradient(135deg, #1baeae 0%, #18a0a0 100%);
  color: white;
  margin: -20px -20px 20px -20px;
}

.card-header h2 {
  margin: 0;
  font-size: 28px;
  font-weight: 600;
  letter-spacing: 2px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.card-header p {
  margin: 10px 0 0;
  font-size: 16px;
  opacity: 0.8;
}

/* 表单样式优化 */
.el-form {
  padding: 0 20px 20px;
}

.custom-input :deep(.el-input__wrapper) {
  border-radius: 12px;
  padding: 12px;
  background: rgba(255, 255, 255, 0.9);
  border: 2px solid transparent;
  transition: all 0.3s ease;
}

.custom-input :deep(.el-input__wrapper:hover) {
  border-color: #1baeae;
}

.custom-input :deep(.el-input__wrapper.is-focus) {
  border-color: #1baeae;
  box-shadow: 0 0 0 2px rgba(27, 174, 174, 0.2);
}

.remember-forgot {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 20px 0;
}

.submit-btn {
  width: 100%;
  height: 48px;
  font-size: 16px;
  font-weight: 600;
  border-radius: 12px;
  background: linear-gradient(135deg, #1baeae 0%, #18a0a0 100%);
  border: none;
  transition: all 0.3s ease;
  margin-top: 10px;
}

.submit-btn:hover {
  background: linear-gradient(135deg, #18a0a0 0%, #159393 100%);
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(27, 174, 174, 0.3);
}

.form-footer {
  text-align: center;
  margin-top: 24px;
}

/* 响应式设计优化 */
@media (max-width: 480px) {
  .content-wrapper {
    margin: 0 16px;
  }
  
  .login-card {
    border-radius: 16px;
  }
  
  .card-header h2 {
    font-size: 24px;
  }
  
  .card-header p {
    font-size: 14px;
  }
  
  .submit-btn {
    height: 44px;
  }
}

/* 自定义复选框样式 */
:deep(.el-checkbox__input.is-checked .el-checkbox__inner) {
  background-color: #1baeae;
  border-color: #1baeae;
}

:deep(.el-checkbox__input.is-checked + .el-checkbox__label) {
  color: #1baeae;
}

/* 自定义链接样式 */
:deep(.el-link.el-link--primary) {
  color: #1baeae;
  font-size: 14px;
  transition: all 0.3s ease;
}

:deep(.el-link.el-link--primary:hover) {
  color: #18a0a0;
  transform: translateX(5px);
}

/* 输入框图标颜色 */
:deep(.el-input__prefix-icon) {
  color: #1baeae;
}
</style>